//
//  MKCustomCalloutView.m
//  UILbrary
//
//  Created by mac on 17/3/9.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "MKCustomCalloutView.h"
#import <QuartzCore/QuartzCore.h>
#import "MKAnnotoationExampleCalloutView.h"


@interface MKCustomCalloutView()


@end


@implementation MKCustomCalloutView

#pragma mark - init
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.translatesAutoresizingMaskIntoConstraints = NO;
        self.inset = UIEdgeInsetsMake(5, 5, 10, 5);
        [self addSubview:self.contentView];
        [self.contentView addSubview:self.backGroundButton];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    //contentView 的约束
    [NSLayoutConstraint activateConstraints:@[[_contentView.topAnchor constraintEqualToAnchor:self.topAnchor constant:self.inset.top / 2.0],
                                              
         [_contentView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor constant:(-self.inset.bottom - self.inset.right/2.0)],
         [_contentView.leftAnchor constraintEqualToAnchor:self.leftAnchor constant:self.inset.left / 2.0],
         [_contentView.rightAnchor constraintEqualToAnchor:self.rightAnchor constant:- self.inset.right / 2.0],
         [_contentView.widthAnchor constraintGreaterThanOrEqualToConstant:self.inset.left + self.inset.right],
         [_contentView.heightAnchor constraintGreaterThanOrEqualToConstant:self.inset.top + self.inset.bottom]]
     ];
    

    //backGroundButton 约束
    [NSLayoutConstraint activateConstraints:@[
                                              [_backGroundButton.topAnchor constraintEqualToAnchor:_contentView.topAnchor],
                                              [_backGroundButton.bottomAnchor constraintEqualToAnchor:_contentView.bottomAnchor],
                                              [_backGroundButton.leadingAnchor constraintEqualToAnchor:_contentView.leadingAnchor],
                                              [_backGroundButton.trailingAnchor constraintEqualToAnchor:_contentView.trailingAnchor]]];

    [self updatePath];
}

- (void)updateConstraints{
    [super updateConstraints];
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
 
    CGPoint contentViewpoint = [self convertPoint:point toView:self.contentView];
    return [self.contentView hitTest:contentViewpoint withEvent:event];
    return nil;
}

/// Add this `CalloutView` to an annotation view (i.e. show the callout on the map above the pin)
///
/// - Parameter annotationView: The annotation to which this callout is being added.



#pragma mark - private methods
- (void)updatePath{
    UIBezierPath *path = [UIBezierPath bezierPath];
    CGPoint point = CGPointMake(self.bounds.size.width - self.inset.right, self.bounds.size.height - self.inset.bottom);
    CGPoint controlPoint;
    [path moveToPoint:point];
    
    switch (self.bubblePointerType) {
        case BubblePointerTypeRounded:
        {
            point = CGPointMake(self.bounds.size.width /2.0 + self.inset.bottom, self.bounds.size.height - self.inset.bottom);
            [path addLineToPoint:point];
            
            //right side of arrow
            
            controlPoint = CGPointMake(self.bounds.size.width / 2.0, self.bounds.size.height - self.inset.bottom);
            point = CGPointMake(self.bounds.size.width/2.0, self.bounds.size.height);
            [path addQuadCurveToPoint:point controlPoint:controlPoint];
            
            //left of pointer
            
            controlPoint = CGPointMake(point.x, self.bounds.size.height - self.inset.bottom);
            point = CGPointMake(point.x - self.inset.bottom, controlPoint.y);
            [path addQuadCurveToPoint:point controlPoint:controlPoint];

        }
            
        case BubblePointerTypeStraight:{
            //lower right
            point = CGPointMake(self.bounds.size.width / 2.0 + tan(self.angle) * self.inset.bottom,self.bounds.size.height - self.inset.bottom);
            [path addLineToPoint:point];
            
            //right side of arrow
            
            point = CGPointMake(self.bounds.size.width / 2.0 - tan(self.angle), self.bounds.size.height - self.inset.bottom);
            [path addLineToPoint:point];
            
            //left of pointer
            
            point = CGPointMake(self.bounds.size.width / 2.0 - tan(self.angle) * self.inset.bottom, self.bounds.size.height - self.inset.bottom);
            [path addLineToPoint:point];

        }
            
            //bottom left
            point.x = self.inset.left;
            
            //lower left corner
            
            controlPoint = CGPointMake(0, self.bounds.size.height - self.inset.bottom);
            point = CGPointMake(0, controlPoint.y - self.inset.left);
            [path addQuadCurveToPoint:point controlPoint:controlPoint];
            
            //left
            
            point.y = self.inset.top;
            [path addLineToPoint:point];
            
            //top left corner
            
            controlPoint = CGPointZero;
            point = CGPointMake(self.inset.left, 0);
            [path addQuadCurveToPoint:point controlPoint:controlPoint];
            
            //top
            
            point = CGPointMake(self.bounds.size.width - self.inset.left, 0);
            [path addLineToPoint:point];
            
            //top right corner
            
            controlPoint = CGPointMake(self.bounds.size.width, 0);
            point = CGPointMake(self.bounds.size.width, self.inset.top);
            [path addQuadCurveToPoint:point controlPoint:controlPoint];
            
            //right
            
            point = CGPointMake(self.bounds.size.width, self.bounds.size.height - self.inset.bottom - self.inset.right);
            [path addLineToPoint:point];
            
            //lower right corner
            
            controlPoint = CGPointMake(self.bounds.size.width, self.bounds.size.height - self.inset.bottom);
            point = CGPointMake(self.bounds.size.width - self.inset.right, self.bounds.size.height - self.inset.bottom);
            [path addQuadCurveToPoint:point controlPoint:controlPoint];
            [path closePath];
            self.bubbleLayer.path = path.CGPath;
       
    }
}
- (void)configContentView{
    
}

#pragma mark - event responde

/// Callout tapped.
///
/// If you want to detect a tap on the callout, override this method. By default, this method does nothing.
///
/// - Parameter sender: The actual hidden button that was tapped, not the callout, itself.
- (void)didTouchUpInCallout{
    
}

#pragma mark - lazy methods
- (UIView *)contentView{
    if (!_contentView) {
        _contentView = [[UIView alloc] initWithFrame:CGRectZero];
        _contentView.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _contentView;
}

- (CAShapeLayer *)bubbleLayer{
    if (!_bubbleLayer) {
        _bubbleLayer = [[CAShapeLayer alloc] init];
        _bubbleLayer.strokeColor = [UIColor blackColor].CGColor;
        _bubbleLayer.fillColor   = [UIColor blueColor].CGColor;
        _bubbleLayer.lineWidth   = 0.5;
    }
    return _bubbleLayer;
}

- (UIButton *)backGroundButton{
    if (!_backGroundButton) {
        _backGroundButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backGroundButton addTarget:self action:@selector(didTouchUpInCallout) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backGroundButton;
}

@end
